package org.mybatis.example;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.*;

/**
 * Using such a TypeHandler would override the existing type handler for Java String properties and VARCHAR parameters and results.请注意，MyBatis不内省数据库元数据来确定类型
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ExampleTypeHandler extends BaseTypeHandler<String> {
  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
    System.out.println("自定义的参数PreparedStatement");
    ps.setString(i,parameter);
  }

  @Override
  public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
    System.out.println("自定义的参数ResultSet");
    return rs.getString(columnName);
  }

  @Override
  public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    System.out.println("自定义的参数");
    return rs.getString(columnIndex);
  }

  @Override
  public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    System.out.println("自定义的参数CallableStatement");
    return cs.getString(columnIndex);
  }
}
